<?php
namespace app\index\controller;

use app\common\controller\UserController;
use app\common\model\EmployPurchase;
use app\common\model\Model;
use app\common\model\ServerGood;

class Server extends UserController
{
    function good(){
        $list = ServerGood::simpleList([
            "query"=>function(Model &$query){
                if(input("server_id")){
                    $query = $query->where("server_id",input("server_id"));
                }
            },
            "result"=>function(&$row){
                $server = \app\common\model\Server::getById($row["server_id"]);
                if(empty($server)){
                    $row = null;
                }else{
                    $row["server_avatar"] = $server->getData("avatar");
                    $row["server_name"] = $server->getData("name");
                    $row["image"] = explode(",",$row["image"]);
                }
            }
        ]);
        $this->result_success($list);
    }
    function server(){
        $list = \app\common\model\Server::simpleList();
        $this->result_success($list);
    }
    function one(){
        $data = \app\common\model\Server::getById(input("id"));
        $this->result_success($data);
    }
    /**
     * 需求
     */
    function good_one(){
        $data = \app\common\model\ServerGood::getById(input("id"));
        if(!empty($data)){
            $server = \app\common\model\Server::getById($data["server_id"]);
            if($server){
                $data["server_avatar"] = $server["avatar"];
                $data["server_name"] = $server["name"];
                $server = $server->toArray();
            }
            $data["image"] = explode(",",$data["image"]);
            $data = array_merge($data->toArray(),$server);
            $this->result_success($data);
        }
        $this->result_error();
    }
    function edit(){
        $param = input();
        $server = self::$user->getServer();
        if(isset( $param["fee"])){
            if($param["fee"]>1000000){
                $this->result_error("交易额不能超过一百万");
            }
        }
        if(empty($server)){
            $this->result_error("请完善服务商信息",510);
        }else{
            $param["server_id"] = $server->id;
            $param["uid"] = $server->uid;
        }
        if(isset($param["id"])){
            \app\common\model\ServerGood::simpleEdit($param);
        }else{
            \app\common\model\ServerGood::simpleAdd($param);
        }
        $this->result_success();
    }
    function del(){
        $id = input("id");
        \app\common\model\ServerGood::simpleDelete(["query"=>function ($query)use($id){
            $query->where("id",$id);
        }]);
        $this->result_success();
    }
}
